﻿
using Lg.Core.Exceptions;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Caching.Memory;
using Snowflake.Core;
using Survey.Core;
using Survey.Web.Filters;
using System.Text.RegularExpressions;
using TemplateEngine;

namespace Survey.Web.Controllers
{
    [ExceptionCatch]
    public class LgmController(IMemoryCache cache,IdWorker idWorker,TemplateServer templateServer) : Controller
    {
        readonly IMemoryCache _cache=cache;
        readonly IdWorker _idWorker=idWorker;
        readonly TemplateServer _templateServer=templateServer;

        [Route("login")]
        [Route("Lgm/login")]
        [HttpGet]
        public async Task<IActionResult> Login()
        {
            return await Task.Run(async () =>
            {
                TemplateManager tm = await _templateServer.GetTemplate("/Lgm/Login");
                if (tm != null)
                {
                    foreach (var query in Request.Query)
                    {
                        tm.SetValue(query.Key, query.Value);
                    }
                    string template = Regex.Replace(tm.Process(), @"\n\s+\n", "\n", RegexOptions.Compiled | RegexOptions.IgnoreCase);
                    return Content(template, "text/html");
                }
                else
                {
                    throw new LgException("模板未找到");
                }
            });


        }

        [Route("Lgm")]
        [Route("Lgm/{action}")]
        [HttpGet]
        public async Task<IActionResult> Index()
        {
            return await Task.Run(async () =>
            {
                TemplateManager tm = await _templateServer.GetTemplate("/Lgm/index");
                if (tm != null)
                {
                    foreach (var query in Request.Query)
                    {
                        tm.SetValue(query.Key, query.Value);
                    }
                    string template = Regex.Replace(tm.Process(), @"\n\s+\n", "\n", RegexOptions.Compiled | RegexOptions.IgnoreCase);
                    return Content(template, "text/html");
                }
                else
                {
                    throw new LgException("模板未找到");
                }
            });


        }
        [Route("Lgm/{path}")]
        [HttpPost]
        public async Task<IActionResult> Free(string path="index")
        {
            return await Task.Run(async() =>
            {
                TemplateManager tm = await _templateServer.GetTemplate($"/Lgm/{path}");
                if (tm != null)
                {
                    foreach (var query in Request.Query)
                    {
                        tm.SetValue(query.Key, query.Value);
                    }
                    string template = Regex.Replace(tm.Process(), @"\n\s+\n", "\n", RegexOptions.Compiled | RegexOptions.IgnoreCase);
                    return Content(template, "text/html");
                }
                else
                {
                    return Content("模版文件未找到");
                }
            });
           
        }

    }
}
